﻿using System;
using System.Collections;
using System.Data;
using System.Windows.Forms;
using AMS.Inventory.PDA.Castle.DataAccess;

namespace AMS.Inventory.PDA
{
    public partial class FrmPlanInfoList : Form
    {
        private readonly SqliteDBServer _sqldb;
        private readonly string _username = "";
        private string _columnsTitle = ""; //DATAGRID表头标题     
        private string _frameSql = "";
        private Hashtable _hashtable;
        private bool _isFinishBound; //记录盘点计划是否绑定完成
        private DataTable _mySourceTable;
        private DataRow currentRow;

        public FrmPlanInfoList()
        {
            InitializeComponent();
        }

        public FrmPlanInfoList(string username, SqliteDBServer sqliteDbServer)
        {
            InitializeComponent();
            _username = username;
            _sqldb = sqliteDbServer;
        }

        private string PlanId
        {
            get { return cboCheckPlan.SelectedValue.ToString(); }
        }

        private void BtnCloseClick(object sender, EventArgs e)
        {
            Close();
        }

        private void FrmCheckInfoList_Load(object sender, EventArgs e)
        {
            _hashtable = clsLocalization.ReadContectResource(Name, ModCommon.DefaultLanguage);
            clsLocalization.getControlNames(this);
            _frameSql = clsLocalization.getFromContentNames(_hashtable, "_frameSql");
            string checkPlanSql = "";
            DataTable checkPlanDt = null;
            checkPlanSql = "SELECT Id,Name FROM AmsInventory_PlanInfo";
            checkPlanDt = _sqldb.GetDataTable(checkPlanSql);
            if (checkPlanDt != null && checkPlanDt.Rows.Count > 0)
            {
                _isFinishBound = false;
                cboCheckPlan.DataSource = checkPlanDt;
                cboCheckPlan.ValueMember = checkPlanDt.Columns["Id"].ToString();
                cboCheckPlan.DisplayMember = checkPlanDt.Columns["Name"].ToString();
                _isFinishBound = true;
                //cboCheckPlan.SelectedIndex = 0;
                //dgrdAssetsInfo.DataSql = _frameSql;
                //dgrdAssetsInfo.CountSql = "SELECT count(*) FROM AmsInventory_PlanInfo ";
                //dgrdAssetsInfo.FrameSql = _frameSql;
                //dgrdAssetsInfo.ReFlashGrid();
            }
        }

        private void CboCheckPlanSelectedIndexChanged(object sender, EventArgs e)
        {
            string planSql = "";
            if (_isFinishBound)
            {
                string planId = "";
                DataTable assetsDt = null;
                if (cboCheckPlan.Text != "")
                    planId = cboCheckPlan.SelectedValue.ToString();
                if (planId != "")
                {
                    dgrdAssetsInfo.DataSql = _frameSql + " where Id='" + planId + "'";
                    dgrdAssetsInfo.CountSql = "SELECT count(*) FROM AmsInventory_PlanInfo where Id='" + planId + "'";
                    dgrdAssetsInfo.FrameSql = _frameSql;
                    dgrdAssetsInfo.ReFlashGrid();
                }
            }
        }

        private void BtnCheckingClick(object sender, EventArgs e)
        {
            if (currentRow == null)
            {
                MessageBox.Show(clsLocalization.getFromContentNames(_hashtable, "ChooseAssetsMsg"));
                return;
            }
            var frmCheckInfoList = new FrmCheckInfoList(_username, _sqldb, PlanId);
            frmCheckInfoList.ShowDialog();
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            string planStr = "", sql;
            planStr = txtCheckplanName.Text.Trim();
            dgrdAssetsInfo.DataSql = _frameSql + "  where Name like '%" + planStr + "%'";
            dgrdAssetsInfo.CountSql = "SELECT count(*) FROM AmsInventory_PlanInfo where  Name like '%" + planStr + "%'";
            dgrdAssetsInfo.FrameSql = _frameSql;
            dgrdAssetsInfo.ReFlashGrid();
        }

        private void dgrdAssetsInfo_CellChanged(object sender, GridClickEventArgs e)
        {
            currentRow = e.Row;
            string colName = clsLocalization.getFromContentNames(_hashtable, "PlanName");
            string planName = currentRow[colName].ToString();
           // cboCheckPlan.Text = planName;
        }
    }
}